package client;

import java.util.ArrayList;

import tools.Card;
import tools.Player;

class BotBadugi
{
	/* wskaznik na klienta utrzymujacego polaczenie */
	private ClientBadugi clientBadugi;
	
	BotBadugi(ClientBadugi clientBadugi)
	{
		this.clientBadugi = clientBadugi;
	}
	
	public void makeDecision(int highestBetInRound, int numberRound, Player player)
	{
		int numberCards = player.getHand().getCards().size();
		int totalChips  = player.getTotalChips();
		int inGameChips = player.getInGameChips(); 
		int step=1;
		
		/*	Mozliwe funkcje do wywolania	
		 * clientControl.bet(bet);
		 * clientControl.check();
		 * clientControl.raise(bet);
		 * clientControl.call();
		 * clientControl.fold();
		 * clientControl.allin();
		 * */
		
		if ((numberCards < 3 && numberRound == 1) || numberCards==3)
		{
			if (highestBetInRound > inGameChips )
			{	
				if(highestBetInRound - inGameChips >= totalChips)
				{
					this.clientBadugi.allin();
				}
				else	
				{
					this.clientBadugi.call();
				}	
			}
			else
			{
				this.clientBadugi.check();
			}
			return;
		}
		
		else if (numberRound == 2 && numberCards < 3)
		{
			this.clientBadugi.fold();
			return;
		}
		
		/* najlepsza opcja */
		if (numberCards == 4)
		{
			int betRaise = highestBetInRound - inGameChips + step;
			if (betRaise >= totalChips)
				this.clientBadugi.allin();
			else
				this.clientBadugi.raise(betRaise);
			return;
		}	
	}
	
	public void exchangeCards(Player player)
	{
		int numberCards = player.getHand().getCards().size();
		ArrayList<Card> cards;
		ArrayList<Card> allCards = player.getHand().getCardList();   
		ArrayList<Card> bestCards = player.getHand().getCards();
		
		if (numberCards == 4)
		{
			this.clientBadugi.clientControl.dontExchange();
		}
		else
		{
			cards = getCardsToExchange(allCards, bestCards);
			this.clientBadugi.clientControl.exchangeCards(cards);
		}
	}
	
	private ArrayList<Card> getCardsToExchange(ArrayList<Card> allCards, ArrayList<Card> bestCards)
	{
		int c=0;
		ArrayList<Card> cards = new ArrayList<Card>();
		boolean isBest;
		
		for(int i=0; i<allCards.size(); i++)
		{
			isBest = false;
			for(int j=0; j<bestCards.size(); j++)
			{
				if (allCards.get(i).sameCard(bestCards.get(j)))
				{
					isBest = true;
					break;
				}
			}
			
			if (!isBest)
				cards.add(c++, allCards.get(i));
		}	
		return cards;
	}
}
